package com.woniu.mgr.controller;


import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.mgr.common.Result;
import com.woniu.mgr.controller.form.OrderModifyForm;
import com.woniu.mgr.model.Account;
import com.woniu.mgr.model.Order;
import com.woniu.mgr.service.AccountService;
import com.woniu.mgr.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 订单信息表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private AccountService accountService;

    @GetMapping("/orderAll")
    public Result orderAll(Integer page, Integer size) {
        log.info("执行了分页查询订单 {}{}", page, size);
        IPage<Order> list = orderService.list(page, size);
        return Result.success(200, "查询成功", list);
    }

    @PutMapping("/orderModify")
    public Result orderModify(OrderModifyForm orderModifyForm) {
        log.info("执行了订单修改操作 {}", orderModifyForm);
        orderService.orderModify(orderModifyForm);
        return Result.success(200, "修改成功", null);
    }

    @GetMapping("/query")
    public Result orderAll(String condition, Integer page, Integer size) {
        log.info("执行了分页条件查询订单{}{}{}", condition, page, size);
        IPage<Order> list = orderService.query(condition, page, size);
        return Result.success(200, "查询成功", list);
    }

//    // 驳回退款
//    @PostMapping("/refuse")
//    public Result refuse(Integer id) {
//        Order byId = orderService.getById(id);
//        byId.setState("已支付待完成");
//        orderService.updateById(byId);
//        return Result.success(200, "已拒绝", null);
//    }
//
//    // 同意退款
//    @PostMapping("/access")
//    public Result access(Integer id) {
//        Order byId = orderService.getById(id);
//        byId.setState("已退款");
//        orderService.updateById(byId);
//        Account account = accountService.getById(byId.getUserId());
//        account.setBalance(account.getBalance().add(byId.getPrice()));
//        accountService.updateById(account);
//        return Result.success(200, "已退款", null);
//    }

    // 同意退款 传入的是订单id
    @PostMapping("/access")
    public Result access(Integer id) {
        orderService.access(id);
        return Result.success(200, "已退款", null);
    }

    // 驳回退款
    @PostMapping("/refuse")
    public Result refuse(Integer id, String reason) {
        orderService.refuse(id, reason);
        return Result.success(200, "退款驳回", null);
    }
}

